home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / CUBE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.7 KB  |  94 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* This program was requested by Patrick Earl; hopefully someone else
  9.    will write the equivalent Direct3D immediate mode program. */
  10.  
  11. #include <GL/glut.h>
  12.  
  13. GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
  14. GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
  15. GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
  16.   {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
  17.   {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
  18. GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
  19.   {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
  20.   {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
  21. GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */
  22.  
  23. void
  24. drawBox(void)
  25. {
  26.   int i;
  27.  
  28.   for (i = 0; i < 6; i++) {
  29.     glBegin(GL_QUADS);
  30.     glNormal3fv(&n[i][0]);
  31.     glVertex3fv(&v[faces[i][0]][0]);
  32.     glVertex3fv(&v[faces[i][1]][0]);
  33.     glVertex3fv(&v[faces[i][2]][0]);
  34.     glVertex3fv(&v[faces[i][3]][0]);
  35.     glEnd();
  36.   }
  37. }
  38.  
  39. void
  40. display(void)
  41. {
  42.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  43.   drawBox();
  44.   glutSwapBuffers();
  45. }
  46.  
  47. void
  48. init(void)
  49. {
  50.   /* Setup cube vertex data. */
  51.   v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  52.   v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  53.   v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  54.   v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  55.   v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  56.   v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
  57.  
  58.   /* Enable a single OpenGL light. */
  59.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  60.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  61.   glEnable(GL_LIGHT0);
  62.   glEnable(GL_LIGHTING);
  63.  
  64.   /* Use depth buffering for hidden surface elimination. */
  65.   glEnable(GL_DEPTH_TEST);
  66.  
  67.   /* Setup the view of the cube. */
  68.   glMatrixMode(GL_PROJECTION);
  69.   gluPerspective( /* field of view in degree */ 40.0,
  70.     /* aspect ratio */ 1.0,
  71.     /* Z near */ 1.0, /* Z far */ 10.0);
  72.   glMatrixMode(GL_MODELVIEW);
  73.   gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
  74.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  75.     0.0, 1.0, 0.);      /* up is in positive Y direction */
  76.  
  77.   /* Adjust cube position to be asthetic angle. */
  78.   glTranslatef(0.0, 0.0, -1.0);
  79.   glRotatef(60, 1.0, 0.0, 0.0);
  80.   glRotatef(-20, 0.0, 0.0, 1.0);
  81. }
  82.  
  83. int
  84. main(int argc, char **argv)
  85. {
  86.   glutInit(&argc, argv);
  87.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  88.   glutCreateWindow("red 3D lighted cube");
  89.   glutDisplayFunc(display);
  90.   init();
  91.   glutMainLoop();
  92.   return 0;             /* ANSI C requires main to return int. */
  93. }
  94.